package br.ufrj.neg.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;

import org.allcolor.yahp.converter.CYaHPConverter;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer.CConvertException;
import org.apache.commons.lang.time.DateFormatUtils;

import br.ufrj.neg.bean.Idioma;

public class Util {

	public static ByteArrayOutputStream HtmlToPdf(String html,String basePath,String baseURL,ByteArrayOutputStream out){
		
		
		CYaHPConverter converter = new CYaHPConverter();
		
		// contains configuration properties
		Map<String,String> properties = new HashMap<String,String>();
		List<String> headerFooterList = new ArrayList<String>();
		
		properties.put(IHtmlToPdfTransformer.PDF_RENDERER_CLASS,
				IHtmlToPdfTransformer.DEFAULT_PDF_RENDERER);
		
		properties.put(IHtmlToPdfTransformer.FOP_TTF_FONT_PATH, basePath + "css" + File.separator+ "fonts" + File.separator+ "Eurosti.ttf");
		
		
		try {		
			converter.convertToPdf(html,
				IHtmlToPdfTransformer.A4P, headerFooterList,baseURL, out,
				properties);
		} catch (CConvertException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		return out;
						
	}
	
	public static String hojeString(Idioma idioma){
		Date dt = new Date();
		long nowLong= dt.getTime();
		String hoje = DateFormatUtils.format(nowLong, "dd/MM/yyyy", idioma.getLocale());
		
		return hoje;
	}
	
	public static Data hojeData(Idioma idioma){
		Date dt = new Date();
		long nowLong= dt.getTime();
		
		Data hoje = new Data(); 
		hoje.setTime(nowLong);
		
		
		return hoje;
	}
	
	
	public static JSONArray concatArray(ArrayList<JSONArray> arrs) throws JSONException {
		JSONArray result = new JSONArray();
		for (JSONArray arr : arrs) {
			for (int i = 0; i < arr.size(); i++) {
				result.element(arr.get(i));
			}
		}
		return result;
	}
	
}
